/**
 * 省市区县JS
 * Created by jiangguangtao on 2016/8/24.
 *
 *
 */


;(function ($) {

    var CityArea = function (options) {
        var opts = $.extend({}, CityArea.defaults, options);
        this.options = opts;
        this.init();
    };

    $.extend(CityArea.prototype, {
        init: function () {
            var options = this.options;

            var provinceObj = this.provinceObj = $(options.provinceId);
            var cityObj = this.cityObj = $(options.cityId);
            var distObj = this.distObj = $(options.distId);
            var hasProvince = this.hasProvince = provinceObj.size() > 0;
            var hasCity =  this.hasCity = cityObj.size() > 0;
            var hasDist = this.hasDist = distObj.size() > 0;

            if(hasProvince) {
                //获取省份列表
                $.getJSON(options.provinceUrl, function (resp) {
                    if (resp.status) {
                        var datas = {};
                        $.each(resp.data, function (i, item) {
                            datas[this.id] = this.areaName;
                        });
                        forms.setSelectOptions(provinceObj, datas, true, false);
                        cityObj.trigger('change');
                    } else {
                        utils.alert(resp.data || '获取省份列表失败！');
                    }
                });

                if(hasCity) {
                    provinceObj.on('change', function(e) {
                        var pval = provinceObj.val();
                        if(utils.isBlank(pval)) {
                            if(hasCity) {
                                cityObj.val('');
                                cityObj.trigger('change');
                            }
                            return;
                        }

                        var pms = {};
                        pms[options.queryCityParamName] = pval;
                        $.getJSON(options.cityUrl, pms, function (resp) {
                            if (resp.status) {
                                var datas = [];
                                $.each(resp.data, function () {
                                    datas[this.id] = this.areaName;
                                });
                                forms.setSelectOptions(cityObj, datas, true, false);
                                cityObj.trigger('change');
                            } else {
                                utils.alert(resp.data || '获取城市列表失败！');
                            }
                        });

                        options.onProvinceChange.call(provinceObj, pval);
                    });
                }
            }

            if(hasCity) {
                if(hasDist) {
                    cityObj.on('change', function (e) {
                        var cval = cityObj.val();
                        if(utils.isBlank(cval)) {
                            if(hasDist) {
                                distObj.val('');
                                distObj.trigger('change');
                            }
                            return;
                        }
                        var pms = { };
                        pms[options.queryDistParamName] = cval;

                        $.getJSON(options.distUrl, pms, function (resp) {
                            if (resp.status) {
                                var datas = [];
                                $.each(resp.data, function () {
                                    datas[this.id] = this.areaName;
                                });
                                forms.setSelectOptions(distObj, datas, true, false);
                                distObj.trigger('change');
                            } else {
                                utils.alert(resp.data || '获取区县列表失败！');
                            }
                        });

                        options.onCityChange.call(cityObj, cval);
                    });
                }
            }
            if(hasDist) {
                distObj.on('change', function (e) {
                    options.onDistChange.call(distObj, distObj.val());
                });
            }
        }
    });

    CityArea.defaults = {
        provinceId: '#provinceId',
        cityId: '#cityId',
        distId: '#distId',

        provinceUrl: ctx + '/common/cityarea/getProvinceList.do',
        cityUrl: ctx + '/common/cityarea/getCityList.do',
        distUrl: ctx + '/common/cityarea/getDistList.do',
        onProvinceChange : function (val) {
            //val 当前值
        },
        onCityChange : function (val) {
            //val 当前值
        },
        onDistChange : function (val) {
            //val 当前值
        },

        queryCityParamName : 'provinceId',
        queryDistParamName : 'cityId'

    };
    window.CityArea = CityArea;
})(jQuery);